package com.weimob.mcloud.wechatwork.customer.domain.enums;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum CorpExternaluserChangEventEnum {
    /**
     * 外部联系人变更相关事件枚举
     */
    ADD(1, "change_external_contact", "add_external_contact", "添加外部联系人", "addCorpExternaluserHandler"),
    EDIT(2, "change_external_contact", "edit_external_contact", "编辑外部联系人", "editCorpExternaluserHandler"),
    DEL(3, "change_external_contact", "del_external_contact", "删除外部联系人", "delCorpExternaluserHandler"),
    //    ADD_HALF(4, "change_external_contact", "add_half_external_contact", "添加外部联系人免登", "addHalfCorpExternaluserHandler"),
    DEL_FOLLOW_USER(5, "change_external_contact", "del_follow_user", "删除跟进成员", "delCorpFollowUserHandler"),
    CREATE_TAG(6, "change_external_tag", "create", "企业客户标签创建", "createTagHandler"),
    UPDATE_TAG(7, "change_external_tag", "update", "企业客户标签变更", "updateTagHandler"),
    DELETE_TAG(8, "change_external_tag", "delete", "企业客户标签删除", "deleteTagHandler"),
    CREATE_CHAT_GROUP(9, "change_external_chat", "create", "客户群创建事件", "createGroupChatHandler"),
    UPDATE_CHAT_GROUP(10, "change_external_chat", "update", "客户群变更事件", "updateGroupChatHandler"),
    DELETE_CHAT_GROUP(11, "change_external_chat", "dismiss", "客户群解散事件", "dismissGroupChatHandler"),
    ;

    private Integer code;
    private String event;
    private String changeType;
    private String desc;
    private String implClassName;

    private static Map<String, Map<String, String>> changeEventMap = Maps.newConcurrentMap();

    static {
        for (CorpExternaluserChangEventEnum value : CorpExternaluserChangEventEnum
                .values()) {
            Map<String, String> changeMap =
                    changeEventMap.computeIfAbsent(value.getEvent(), obj -> new ConcurrentHashMap<>());
            changeMap.put(value.getChangeType(), value.getImplClassName());
        }
    }

    /**
     * 因为渠道原因 该方法不能获取添加外部联系人handler
     *
     * @param infoType
     * @param changeType
     * @return
     */
    public static String getClassNameByTypeExcludeAdd(String infoType, String changeType) {
        if (StringUtils.isBlank(infoType) || StringUtils.isBlank(changeType)) {
            return "";
        }
        if (infoType.equals(ADD.getEvent()) && infoType.equals(ADD.getChangeType())) {
            return "";
        }
        Map<String, String> infoMap = changeEventMap.get(infoType);
        if (infoMap == null) {
            return "";
        }
        return infoMap.get(changeType);
    }


    /**
     * 通过changeType获取code
     *
     * @param changeType changeType
     * @return code
     */
    public static Integer getCodeByChangeType(String changeType) {
        if (StringUtils.isBlank(changeType)) {
            return null;
        }
        CorpExternaluserChangEventEnum[] values = CorpExternaluserChangEventEnum.values();
        for (CorpExternaluserChangEventEnum value : values) {
            if (value.changeType.equals(changeType)) {
                return value.getCode();
            }
        }
        return null;
    }
}
